package com.inspire.tank.cor;

import com.inspire.tank.*;

/**
 * @author inspire
 * @description 计算子弹和坦克相撞
 * @date 2022/6/15 23:01
 */
public class BulletTankCollider implements Collider {
    @Override
    public boolean collide(GameObject gameObject1, GameObject gameObject2) {

        if (gameObject1 instanceof Bullet && gameObject2 instanceof Tank) {
            Bullet bullet = (Bullet) gameObject1;
            Tank tank = (Tank) gameObject2;
            if(bullet.group == tank.getGroup()){//自己的子弹不打自己的坦克
                return true;
            }
            if(bullet.rect.intersects(tank.rect)){
                bullet.die();

                if(tank.group==Group.BAD){//敌军坦克打不死主战坦克
                    tank.die();
                }
                int eX=tank.getX()+Tank.WIDTH/2- Explode.WIDTH/2;
                int eY=tank.getY()+Tank.HEIGHT/2-Explode.HEIGHT/2;
                new Explode(eX,eY);
                return false;
            }
            /*if(bullet.collidWidth(tank)){
                return false;//碰撞上了，不再执行，返回false
            }*/
        } else if (gameObject1 instanceof Tank && gameObject2 instanceof Bullet) {
            return collide(gameObject2, gameObject1);
        }
        return true;


    }
}
